
package jagex.client;

import jagex.DataFileDecrypter;
import jagex.DataOperations;
import java.applet.Applet;
import java.awt.*;
import java.awt.image.IndexColorModel;
import java.awt.image.MemoryImageSource;
import java.io.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class GameWindow extends Applet implements Runnable {

	protected void startGameClient() {
	}

	public synchronized void dj() {
	}

	public void garbageCollect() {
	}

	public synchronized void jj() {
	}

	public void sj() {
	}

	public final void createWindow(int arg0, int arg1, String arg2, boolean arg3) {
		appletMode = true;
		System.out.println("Started application");
		appletWidth = arg0;
		appletHeight = arg1;
		gameFrame = new GameFrame(this, arg0, arg1, arg2, arg3, false);
		loadingScreen = 1;
		gameWindowThread = new Thread(this);
		gameWindowThread.start();
		gameWindowThread.setPriority(1);
	}

	public final boolean qj() {
		return appletMode;
	}

	public final void fj(int arg0, int arg1, String arg2, boolean arg3) {
		if (gameFrame != null) {
			return;
		} else {
			appletWidth = arg0;
			appletHeight = arg1;
			gameFrame = new GameFrame(this, arg0, arg1, arg2, arg3, appletMode);
			return;
		}
	}

	public final void vi(int arg0, int arg1) {
		if (gameFrame == null) {
			return;
		} else {
			gameFrame.resize(arg0, arg1);
			appletWidth = arg0;
			appletHeight = arg1;
			return;
		}
	}

	public final void xi(Image arg0) {
		if (gameFrame == null) {
			return;
		} else {
			gameFrame.setIconImage(arg0);
			return;
		}
	}

	public void mj(int arg0, int arg1) {
		vi(arg0, arg1);
	}

	public final Graphics getGraphics() {
		if (gameFrame == null) {
			return super.getGraphics();
		} else {
			return gameFrame.getGraphics();
		}
	}

	public final int ej() {
		return appletWidth;
	}

	public final int oj() {
		return appletHeight;
	}

	public final Image createImage(int arg0, int arg1) {
		if (gameFrame == null) {
			return super.createImage(arg0, arg1);
		} else {
			return gameFrame.createImage(arg0, arg1);
		}
	}

	public Frame ij() {
		return gameFrame;
	}

	public final void lj(int arg0) {
		threadSleepModifier = 1000 / arg0;
	}

	public final void nj(int arg0) {
		lp = arg0;
	}

	public final void pj() {
		for (int j = 0; j < 10; j++) {
			currentTimeArray[j] = 0L;
		}

	}

	public synchronized boolean keyDown(Event event, int key) {
		handleMenuKeyDown(key);
		keyDown = key;
		rq = key;
		lastActionTimeout = 0;
		if (key == 1006) {
			keyLeftDown = true;
		}
		if (key == 1007) {
			keyRightDown = true;
		}
		if (key == 1004) {
			keyUpDown = true;
		}
		if (key == 1005) {
			keyDownDown = true;
		}
		if ((char) key == ' ') {
			keySpaceDown = true;
		}
		if ((char) key == 'n' || (char) key == 'm') {
			keyNMDown = true;
		}
		if ((char) key == 'N' || (char) key == 'M') {
			keyNMDown = true;
		}
		if ((char) key == '{') {
			keyLeftBraceDown = true;
		}
		if ((char) key == '}') {
			keyRightBraceDown = true;
		}
		if ((char) key == '\u03F0') {
			keyF1Toggle = !keyF1Toggle;
		}
		if ((key >= 97 && key <= 122 || key >= 65 && key <= 90 || key >= 48 && key <= 57 || key == 32) && inputText.length() < 16) {
			inputText += (char) key;
		}
		if (key >= 32 && key <= 122 && inputMessage.length() < 80) {
			inputMessage += (char) key;
		}
		if (key == 8 && inputText.length() > 0) {
			inputText = inputText.substring(0, inputText.length() - 1);
		}
		if (key == 8 && inputMessage.length() > 0) {
			inputMessage = inputMessage.substring(0, inputMessage.length() - 1);
		}
		if (key == 10 || key == 13) {
			enteredText = inputText;
			enteredMessage = inputMessage;
		}
		return true;
	}

	public void handleMenuKeyDown(int key) {
	}

	public synchronized boolean keyUp(Event event, int arg1) {
		keyDown = 0;
		if (arg1 == 1006) {
			keyLeftDown = false;
		}
		if (arg1 == 1007) {
			keyRightDown = false;
		}
		if (arg1 == 1004) {
			keyUpDown = false;
		}
		if (arg1 == 1005) {
			keyDownDown = false;
		}
		if ((char) arg1 == ' ') {
			keySpaceDown = false;
		}
		if ((char) arg1 == 'n' || (char) arg1 == 'm') {
			keyNMDown = false;
		}
		if ((char) arg1 == 'N' || (char) arg1 == 'M') {
			keyNMDown = false;
		}
		if ((char) arg1 == '{') {
			keyLeftBraceDown = false;
		}
		if ((char) arg1 == '}') {
			keyRightBraceDown = false;
		}
		return true;
	}

	public synchronized boolean mouseMove(Event event, int arg1, int arg2) {
		mouseX = arg1;
		mouseY = arg2 + yOffset;
		mouseDownButton = 0;
		lastActionTimeout = 0;
		return true;
	}

	public synchronized boolean mouseUp(Event arg0, int arg1, int arg2) {
		mouseX = arg1;
		mouseY = arg2 + yOffset;
		mouseDownButton = 0;
		return true;
	}

	public synchronized boolean mouseDown(Event arg0, int arg1, int arg2) {
		mouseX = arg1;
		mouseY = arg2 + yOffset;
		if (arg0.metaDown()) {
			mouseDownButton = 2;
		} else {
			mouseDownButton = 1;
		}
		pq = mouseDownButton;
		lastActionTimeout = 0;
		return true;
	}

	public synchronized boolean mouseDrag(Event arg0, int arg1, int arg2) {
		mouseX = arg1;
		mouseY = arg2 + yOffset;
		if (arg0.metaDown()) {
			mouseDownButton = 2;
		} else {
			mouseDownButton = 1;
		}
		return true;
	}

	public final void init() {
		appletMode = true;
		System.out.println("Started applet");
		appletWidth = 512;
		appletHeight = 334;
		loadingScreen = 1;

		startThread(this);
	}

	protected Socket makeSocket(String address, int port) throws IOException {
		Socket socket = new Socket(InetAddress.getByName(address), port);
		socket.setSoTimeout(30000);
		socket.setTcpNoDelay(true);
		return socket;
	}

	protected void startThread(Runnable runnable) {
		Thread thread = new Thread(runnable);
		thread.setDaemon(true);
		thread.start();
	}

	public final void start() {
		if (exitTimeout >= 0) {
			exitTimeout = 0;
		}
	}

	public final void stop() {
		if (exitTimeout >= 0) {
			exitTimeout = 4000 / threadSleepModifier;
		}
	}

	public final void destroy() {
		exitTimeout = -1;
		try {
			Thread.sleep(5000L);
		} catch (Exception _ex) {
		}
		if (exitTimeout == -1) {
			System.out.println("5 seconds expired, forcing kill");
			close();
			if (gameWindowThread != null) {
				gameWindowThread.stop();
				gameWindowThread = null;
			}
		}
	}

	public final void close() {
		exitTimeout = -2;
		System.out.println("Closing program");
		garbageCollect();
		try {
			Thread.sleep(1000L);
		} catch (Exception _ex) {
		}
		if (gameFrame != null) {
			gameFrame.dispose();
		}
		if (!appletMode) {
			System.exit(0);
		}
	}

	public final void run() {
		if (loadingScreen == 1) {
			loadingScreen = 2;
			loadingGraphics = getGraphics();
			drawLoadingLogo();
			drawLoadingScreen(0, "Loading...");
			startGameClient();
			loadingScreen = 0;
		}
		int j = 0;
		int i1 = 256;
		int sleepTime = 1;
		int k1 = 0;
		for (int i2 = 0; i2 < 10; i2++) {
			currentTimeArray[i2] = System.currentTimeMillis();
		}

		long l = System.currentTimeMillis();
		while (exitTimeout >= 0) {
			if (exitTimeout > 0) {
				exitTimeout--;
				if (exitTimeout == 0) {
					close();
					gameWindowThread = null;
					return;
				}
			}
			int j2 = i1;
			int k2 = sleepTime;
			i1 = 300;
			sleepTime = 1;
			long l1 = System.currentTimeMillis();
			if (currentTimeArray[j] == 0L) {
				i1 = j2;
				sleepTime = k2;
			} else if (l1 > currentTimeArray[j]) {
				i1 = (int) ((long) (2560 * threadSleepModifier) / (l1 - currentTimeArray[j]));
			}
			if (i1 < 25) {
				i1 = 25;
			}
			if (i1 > 256) {
				i1 = 256;
				sleepTime = (int) ((long) threadSleepModifier - (l1 - currentTimeArray[j]) / 10L);
				if (sleepTime < threadSleepTime) {
					sleepTime = threadSleepTime;
				}
			}
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException _ex) {
			}
			currentTimeArray[j] = l1;
			j = (j + 1) % 10;
			if (sleepTime > 1) {
				for (int l2 = 0; l2 < 10; l2++) {
					if (currentTimeArray[l2] != 0L) {
						currentTimeArray[l2] += sleepTime;
					}
				}

			}
			int i3 = 0;
			while (k1 < 256) {
				dj();
				k1 += i1;
				if (++i3 > lp) {
					k1 = 0;
					qp += 6;
					if (qp > 25) {
						qp = 0;
						keyF1Toggle = true;
					}
					break;
				}
			}
			qp--;
			k1 &= 0xff;
			jj();
			xq = (1000 * i1) / (threadSleepModifier * 256);
			if (appletMode && j == 0) {
				// showStatus("Fps:" + xq + "Del:" + j1);
				if (gameFrame != null && (gameFrame.o() != appletWidth || gameFrame.p() != appletHeight)) {
					mj(gameFrame.o(), gameFrame.p());
				}
			}
		}
		if (exitTimeout == -1) {
			close();
		}
		gameWindowThread = null;
	}

	public final void update(Graphics graphic) {
		paint(graphic);
	}

	public final void paint(Graphics graphic) {
		if (loadingScreen == 2 && loadingLogo != null) {
			drawLoadingScreen(wp, loadingBarText);
			return;
		}
		if (loadingScreen == 0) {
			sj();
		}
	}

	public void drawLoadingLogo() {
		try {
			byte abyte0[] = DataOperations.pm("gamedata/jagex.jag");
			byte abyte1[] = DataOperations.lm("logo.tga", 0, abyte0);
			loadingLogo = bj(abyte1);
			GameImage.ye(DataOperations.lm("h11p.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h12b.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h12p.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h13b.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h14b.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h16b.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h20b.jf", 0, abyte0));
			GameImage.ye(DataOperations.lm("h24b.jf", 0, abyte0));
			return;
		} catch (IOException _ex) {
			System.out.println("Error loading jagex.dat");
		}
	}

	public void drawLoadingScreen(int arg0, String arg1) {
		int j = (appletWidth - 281) / 2;
		int l = (appletHeight - 148) / 2;
		loadingGraphics.setColor(Color.black);
		loadingGraphics.fillRect(0, 0, appletWidth, appletHeight);
		if (!vp) {
			loadingGraphics.drawImage(loadingLogo, j, l, this);
		}
		j += 2;
		l += 90;
		wp = arg0;
		loadingBarText = arg1;
		loadingGraphics.setColor(new Color(132, 132, 132));
		if (vp) {
			loadingGraphics.setColor(new Color(220, 0, 0));
		}
		loadingGraphics.drawRect(j - 2, l - 2, 280, 23);
		loadingGraphics.fillRect(j, l, (277 * arg0) / 100, 20);
		loadingGraphics.setColor(new Color(198, 198, 198));
		if (vp) {
			loadingGraphics.setColor(new Color(255, 255, 255));
		}
		zi(loadingGraphics, arg1, loadingFont, j + 138, l + 10);
		if (!vp) {
			zi(loadingGraphics, "Created by JAGeX - visit www.jagex.com", loadingScreenFont1, j + 138,
					l + 30);
			zi(loadingGraphics, "Copyright \2512000 Andrew Gower", loadingScreenFont1, j + 138, l + 44);
		} else {
			loadingGraphics.setColor(new Color(132, 132, 152));
			zi(loadingGraphics, "Copyright \2512000 Andrew Gower", loadingScreenFont2, j + 138,
					appletHeight - 20);
		}
		if (up != null) {
			loadingGraphics.setColor(Color.white);
			zi(loadingGraphics, up, loadingScreenFont1, j + 138, l - 120);
		}
	}

	public void drawProgressBar(int percent, String message) {
		int j = (appletWidth - 281) / 2;
		int l = (appletHeight - 148) / 2;
		j += 2;
		l += 90;
		wp = percent;
		loadingBarText = message;
		int i1 = (277 * percent) / 100;
		loadingGraphics.setColor(new Color(132, 132, 132));
		if (vp) {
			loadingGraphics.setColor(new Color(220, 0, 0));
		}
		loadingGraphics.fillRect(j, l, i1, 20);
		loadingGraphics.setColor(Color.black);
		loadingGraphics.fillRect(j + i1, l, 277 - i1, 20);
		loadingGraphics.setColor(new Color(198, 198, 198));
		if (vp) {
			loadingGraphics.setColor(new Color(255, 255, 255));
		}
		zi(loadingGraphics, message, loadingFont, j + 138, l + 10);
	}

	public void zi(Graphics arg0, String arg1, Font arg2, int arg3, int arg4) {
		Object obj;
		if (gameFrame == null) {
			obj = this;
		} else {
			obj = gameFrame;
		}
		FontMetrics fontmetrics = ((Component) (obj)).getFontMetrics(arg2);
		fontmetrics.stringWidth(arg1);
		arg0.setFont(arg2);
		arg0.drawString(arg1, arg3 - fontmetrics.stringWidth(arg1) / 2, arg4
				+ fontmetrics.getHeight() / 4);
	}

	public Image bj(byte arg0[]) {
		int j = arg0[13] * 256 + arg0[12];
		int l = arg0[15] * 256 + arg0[14];
		byte abyte0[] = new byte[256];
		byte abyte1[] = new byte[256];
		byte abyte2[] = new byte[256];
		for (int i1 = 0; i1 < 256; i1++) {
			abyte0[i1] = arg0[20 + i1 * 3];
			abyte1[i1] = arg0[19 + i1 * 3];
			abyte2[i1] = arg0[18 + i1 * 3];
		}

		IndexColorModel indexcolormodel = new IndexColorModel(8, 256, abyte0,
				abyte1, abyte2);
		byte abyte3[] = new byte[j * l];
		int j1 = 0;
		for (int k1 = l - 1; k1 >= 0; k1--) {
			for (int l1 = 0; l1 < j; l1++) {
				abyte3[j1++] = arg0[786 + l1 + k1 * j];
			}

		}

		MemoryImageSource memoryimagesource = new MemoryImageSource(j, l,
				indexcolormodel, abyte3, 0, j);
		Image image = createImage(memoryimagesource);
		return image;
	}

	public byte[] updareProgressBar(String string, String loadingString, int stage) throws IOException {
		int j = 0;
		int l = 0;
		int i1 = 0;
		byte abyte0[] = null;
		while (j < 2) {
			try {
				drawProgressBar(stage, "Loading " + loadingString + " - 0%");
				if (j == 1) {
					string = string.toUpperCase();
				}
				java.io.InputStream inputstream = DataOperations.streamFromPath("gamedata" + File.separator + string);
				DataInputStream datainputstream = new DataInputStream(inputstream);
				byte abyte2[] = new byte[6];
				datainputstream.readFully(abyte2, 0, 6);
				l = ((abyte2[0] & 0xff) << 16) + ((abyte2[1] & 0xff) << 8) + (abyte2[2] & 0xff);
				i1 = ((abyte2[3] & 0xff) << 16) + ((abyte2[4] & 0xff) << 8) + (abyte2[5] & 0xff);
				drawProgressBar(stage, "Loading " + loadingString + " - 5%");
				int j1 = 0;
				abyte0 = new byte[i1];
				while (j1 < i1) {
					int k1 = i1 - j1;
					if (k1 > 1000) {
						k1 = 1000;
					}
					datainputstream.readFully(abyte0, j1, k1);
					j1 += k1;
					drawProgressBar(stage, "Loading " + loadingString + " - " + (5 + (j1 * 95) / i1) + "%");
				}
				j = 2;
				datainputstream.close();
			} catch (IOException _ex) {
				j++;
			}
		}
		drawProgressBar(stage, "Unpacking " + loadingString);
		if (i1 != l) {
			byte abyte1[] = new byte[l];
			DataFileDecrypter.unpackData(abyte1, l, abyte0, i1, 0);
			return abyte1;
		} else {
			return abyte0;
		}
	}

	public GameWindow() {
		appletWidth = 512;
		appletHeight = 384;
		threadSleepModifier = 20;
		lp = 1000;
		currentTimeArray = new long[10];
		loadingScreen = 1;
		vp = false;
		loadingBarText = "Loading";
		loadingFont = new Font("TimesRoman", 0, 15);
		loadingScreenFont1 = new Font("Helvetica", 1, 13);
		loadingScreenFont2 = new Font("Helvetica", 0, 12);
		keyLeftBraceDown = false;
		keyRightBraceDown = false;
		keyLeftDown = false;
		keyRightDown = false;
		keyUpDown = false;
		keyDownDown = false;
		keySpaceDown = false;
		keyNMDown = false;
		threadSleepTime = 1;
		keyF1Toggle = false;
		inputText = "";
		enteredText = "";
		inputMessage = "";
		enteredMessage = "";
	}

	private int appletWidth;
	private int appletHeight;
	private Thread gameWindowThread;
	private int threadSleepModifier;
	private int lp;
	private long currentTimeArray[];
	static GameFrame gameFrame = null;
	private boolean appletMode;
	private int exitTimeout;
	private int qp;
	public int yOffset;
	public int lastActionTimeout;
	public int loadingScreen;
	public String up;
	private boolean vp;
	private int wp;
	private String loadingBarText;
	private Font loadingFont;
	private Font loadingScreenFont1;
	private Font loadingScreenFont2;
	private Image loadingLogo;
	private Graphics loadingGraphics;
	public boolean keyLeftBraceDown;
	public boolean keyRightBraceDown;
	public boolean keyLeftDown;
	public boolean keyRightDown;
	public boolean keyUpDown;
	public boolean keyDownDown;
	public boolean keySpaceDown;
	public boolean keyNMDown;
	public int threadSleepTime;
	public int mouseX;
	public int mouseY;
	public int mouseDownButton;
	public int pq;
	public int keyDown;
	public int rq;
	public boolean keyF1Toggle;
	public String inputText;
	public String enteredText;
	public String inputMessage;
	public String enteredMessage;
	public int xq;

}
